
import java.awt.*;
import java.awt.event.*;
//import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.*;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;

public class StudentSystem extends JFrame implements ActionListener {//继承自JFrame使得这个类成为一个窗体,可以对窗体的属性进行扩展并且可以定义自己需要的特殊操作方法
	private static final long serialVersionUID = 1L;//把java对象序列化而后进行保存
	private Map<String, String> PersonInfo;
	public static Vector<Vector<String>> info = new Vector<Vector<String>>();
	private JLabel keyLab;
	private JButton searchBtn, createBtn, updateBtn, deleteBtn,exitBtn;
	public static JTable infoTable;
	private JTextField keyText;
	public static Vector<String> column;

	public StudentSystem() {
		PersonInfo = new HashMap<String, String>();//数组和链表的结合体,HashMap底层就是一个数组结构，数组中的每一项又是一个链表。新建一个HashMap的时候，就会初始化一个数组
		Font font = new Font("宋体", Font.PLAIN, 15);//设置字体，类型和大小；Front.PLAIN普通，Front.BLOD加粗，Front.ITALIC斜体
		JPanel pNorth = new JPanel();
		pNorth.setLayout(new FlowLayout(FlowLayout.RIGHT));
		keyLab = new JLabel("请输入关键字：");
		keyText = new JTextField(10);//搜索文本框
		
		//创建系统功能按钮
		searchBtn = new JButton("搜索学生信息");
		createBtn = new JButton("新增学生信息");
		updateBtn = new JButton("修改学生信息");
		deleteBtn = new JButton("删除学生信息");
		exitBtn = new JButton("退出系统");
		
		//设置字体大小
		keyLab.setFont(font);
		searchBtn.setFont(font);		
		createBtn.setFont(font);		
		updateBtn.setFont(font);		
		deleteBtn.setFont(font);
		exitBtn.setFont(font);
		
		//添加监听器
		searchBtn.addActionListener(this);
		createBtn.addActionListener(this);
		updateBtn.addActionListener(this);
		deleteBtn.addActionListener(this);
		exitBtn.addActionListener(this);
		
		//在JPanel面板的上方加入搜索功能所需的一系列组件
		pNorth.add(keyLab);
		pNorth.add(keyText);
		pNorth.add(searchBtn);

		//在JPanel面板下方加入系统功能组件
		JPanel pSouth = new JPanel();
		pSouth.add(createBtn);
		pSouth.add(updateBtn);
		pSouth.add(deleteBtn);
		pSouth.add(exitBtn);

		//表格数据
		column = new Vector<String>();
		column.add("编号");
		column.add("姓名");
		column.add("性别");
		column.add("电话");
		column.add("学号");
		column.add("生日");
		column.add("政治面貌");
		column.add("地址");
		column.add("宿舍号");
		
		flashInfo();//将数据存入数据库
		infoTable = new JTable(info, column);
		TableColumn column1 = infoTable.getColumnModel().getColumn(0);
		column1.setPreferredWidth(30);//自适应

		TableColumn column3 = infoTable.getColumnModel().getColumn(2);
		column3.setPreferredWidth(30);//自适应

		JScrollPane pCenter = new JScrollPane(infoTable);//创建垂直滚动面板
		this.add(pNorth, "North");
		this.add(pCenter, "Center");
		this.add(pSouth, "South");

		this.setTitle("学生信息管理系统");
		this.setSize(800, 450);
		this.setVisible(true);
		this.setLocationRelativeTo(null);
		this.setResizable(false);
		this.setDefaultCloseOperation(EXIT_ON_CLOSE);
	}

	public static void flashInfo() {
		Connect dbconn = new Connect();
		Statement stmt = null;
		ResultSet rs = null;
		info.clear();
		try {
			stmt = dbconn.getConnection().createStatement();//创建一个 Statement 对象，封装 SQL 语句发送给数据库
			rs = stmt.executeQuery("select * from my_address_book");//下达命令执行查询语句并且存放在ResultSet对象中
			while (rs.next()) {
				Vector<String> row = new Vector<String>();
				row.add(rs.getString(1));
				row.add(rs.getString(2));
				row.add(rs.getString(3));
				row.add(rs.getString(4));
				row.add(rs.getString(5));
				row.add(rs.getString(6));
				row.add(rs.getString(7));
				row.add(rs.getString(8));
				row.add(rs.getString(9));
				info.add(row);
			}
			rs.close();
		} catch (SQLException e) {
			e.printStackTrace();//在命令行打印异常信息在程序中出错的位置及原因
		} finally {
			try {
				if (stmt != null) {
					stmt.close();
				}
				if (rs != null) {
					rs.close();
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}

	public void actionPerformed(ActionEvent e) {
		int rowNum = infoTable.getSelectedRow();//返回第一个选定行的索引
		if (rowNum != -1) {
			PersonInfo = new HashMap<String, String>();
			//将值插入HasMap中
			PersonInfo.put("id", (String) infoTable.getValueAt(rowNum, 0));//返回表格row和column位置的单元格值
			PersonInfo.put("name", (String) infoTable.getValueAt(rowNum, 1));
			PersonInfo.put("sex", (String) infoTable.getValueAt(rowNum, 2));
			PersonInfo.put("telephone", (String) infoTable.getValueAt(rowNum, 3));
			PersonInfo.put("number", (String) infoTable.getValueAt(rowNum, 4));
			PersonInfo.put("birthday", (String) infoTable.getValueAt(rowNum, 5));
			PersonInfo.put("note", (String) infoTable.getValueAt(rowNum, 6));
			PersonInfo.put("address", (String) infoTable.getValueAt(rowNum, 7));
			PersonInfo.put("dorm", (String) infoTable.getValueAt(rowNum, 8));
		}

		if (e.getSource() == searchBtn) {//搜索
			String keyStr = keyText.getText();
			searchInfo(keyStr);
		} else if (e.getSource() == createBtn) {//新建
			MyDialog InsertPane = new MyDialog("新建学生信息", new HashMap<String, String>());
			InsertPane.setVisible(true);
		} else if (e.getSource() == updateBtn) {//修改
			if (rowNum == -1) {
				JOptionPane.showMessageDialog(null, "请选择学生");//提示弹窗
			}
			MyDialog UpdatePane = new MyDialog("修改学生信息", PersonInfo);
			UpdatePane.setVisible(true);
		} else if (e.getSource() == deleteBtn) {//删除
			if (rowNum == -1) {
				JOptionPane.showMessageDialog(null, "请选择学生");//提示弹窗
			}
			MyDialog DeletePane = new MyDialog("删除学生信息", PersonInfo);
			DeletePane.setVisible(true);
		}else if(e.getSource()==exitBtn) {//退出
			this.setVisible(false);
		}
	}

	protected void searchInfo(String key) {//搜索
		Connect dbconn = new Connect();
		Statement stmt = null;
		ResultSet rs = null;
		try {
			stmt = dbconn.getConnection().createStatement();
			String sql = "select * from my_address_book where name like'%" + key + "%'";
			
			
			rs = stmt.executeQuery(sql);
		
			info.clear();
			while (rs.next()) {
				Vector<String> row = new Vector<String>();//创建自增长数组
				row.add(rs.getString(1));//向Vector中添加值
				row.add(rs.getString(2));
				row.add(rs.getString(3));
				row.add(rs.getString(4));
				row.add(rs.getString(5));
				row.add(rs.getString(6));
				row.add(rs.getString(7));
				row.add(rs.getString(8));
				info.add(row);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				if (stmt != null) {
					stmt.close();
				}
				if (rs != null) {
					rs.close();
				}

			} catch (SQLException e) {
				e.printStackTrace();
			}
			DefaultTableModel model = new DefaultTableModel(StudentSystem.info, StudentSystem.column);//构造一个 DefaultTableModel，并通过将 data 和 columnNames 传递到 setDataVector 方法来初始化该表。
			StudentSystem.infoTable.setModel(model);//数据绑定
			TableColumn column1 = StudentSystem.infoTable.getColumnModel().getColumn(0);
			column1.setMaxWidth(40);
			column1.setMinWidth(40);

			TableColumn column3 = StudentSystem.infoTable.getColumnModel().getColumn(2);
			column3.setMaxWidth(40);
			column3.setMinWidth(40);
		}
	}

	public static void main(String[] args) {
		new StudentSystem();
	}
}